package me.sealer.ssm.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import me.sealer.ssm.constant.PageConstant;
import me.sealer.ssm.constant.RoleConstant;
import me.sealer.ssm.controller.dto.CommonResponse;
import me.sealer.ssm.controller.dto.DeletePermissionRequest;
import me.sealer.ssm.controller.dto.NewPermissionRequest;
import me.sealer.ssm.model.Permission;
import me.sealer.ssm.page.PageResult;
import me.sealer.ssm.service.PermissionService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 功能描述： $ PermissionController
 *
 * @author sealer
 * @email 1178884049@qq.com
 * @date 2020年01月08日 13时37分11秒
 */
@Slf4j
@RestController
@Api(tags = "权限管理相关api")
@RequestMapping("/permissionOperations")
@RequiresRoles(RoleConstant.ROLE_ADMIN)
public class PermissionController {
    private final PermissionService permissionService;

    public PermissionController(PermissionService permissionService) {
        this.permissionService = permissionService;
    }

    @ApiOperation(value = "分页查询所有权限")
    @GetMapping(value = "/queryPermissionsPage", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResponse<PageResult<Permission>> queryPermissionsPage(
            @ApiParam(value = "第几页", required = true) @RequestParam(PageConstant.PAGE_NO) Integer pageNo,
            @ApiParam(value = "每页几条数据", required = true) @RequestParam(PageConstant.PAGE_SIZE) Integer pageSize) {

        PageResult<Permission> pageResult = permissionService.queryPermissionsPage(pageNo, pageSize);
        return new CommonResponse<>(pageResult);
    }

    @ApiOperation(value = "查询所有权限")
    @GetMapping(value = "/queryPermissions", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResponse<List<Permission>> queryPermissions() {

        List<Permission> permissions = permissionService.queryAllPermissions();
        return new CommonResponse<>(permissions);
    }

    @ApiOperation(value = "新增权限")
    @PostMapping(value = "/newPermission", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResponse newPermission(@Valid @RequestBody NewPermissionRequest newPermissionRequest) {

        permissionService.newPermission(Permission.builder()
                .name(newPermissionRequest.getName())
                .permission(newPermissionRequest.getPermission())
                .description(newPermissionRequest.getDescription())
                .build());
        return new CommonResponse();
    }

    @ApiOperation(value = "删除权限")
    @PostMapping(value = "/deletePermission", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResponse deletePermission(@Valid @RequestBody DeletePermissionRequest deletePermissionRequest) {

        permissionService.deletePermission(deletePermissionRequest.getId(), deletePermissionRequest.getPermission());
        return new CommonResponse();
    }
}
